﻿using Microsoft.Xna.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;

namespace WorseGame.Content
{
    public class 雷电破坏系统(IMonitor monitor, IModHelper helper)
    {
        public readonly IMonitor 监视器 = monitor;
        public readonly IModHelper 助手 = helper;
        private readonly double 破坏概率 = 0.3; 
        private bool 已加载存档 = false;

        public void Initialize()
        {
            助手.Events.GameLoop.DayStarted += 每日开始;
            助手.Events.GameLoop.SaveLoaded += 存档加载;
            助手.Events.GameLoop.ReturnedToTitle += 返回标题;
            助手.Events.GameLoop.TimeChanged += 时间变化;
        }

        public void 存档加载(object? sender, SaveLoadedEventArgs e)
        {
            已加载存档 = true;
        }

        public void 返回标题(object? sender, ReturnedToTitleEventArgs e)
        {
            已加载存档 = false;
        }

        public void 每日开始(object? sender, DayStartedEventArgs e)
        {
            if (!已加载存档) return;

            if (Game1.isLightning)
            {
                监视器.Log("今天是雷雨天气，可能会破坏避雷针", LogLevel.Info);
            }
        }

        public void 时间变化(object? sender, TimeChangedEventArgs e)
        {
            if (!已加载存档) return;
            if (Game1.isLightning && e.NewTime % 100 == 0)
            {
                检查避雷针破坏();
            }
        }

        private void 检查避雷针破坏()
        {
            try
            {
                var 所有区域 = new List<GameLocation>
                {
                    Game1.getLocationFromName("Farm"),
                    Game1.getLocationFromName("IslandWest"),
                    Game1.getLocationFromName("Greenhouse"),
                    Game1.getLocationFromName("Shed"),
                    Game1.getLocationFromName("Big Shed"),
                    Game1.getLocationFromName("Barn"),
                    Game1.getLocationFromName("Big Barn"),
                    Game1.getLocationFromName("Deluxe Barn"),
                    Game1.getLocationFromName("Coop"),
                    Game1.getLocationFromName("Big Coop"),
                    Game1.getLocationFromName("Deluxe Coop")
                };

                int 总破坏数量 = 0;

                foreach (var 区域 in 所有区域)
                {
                    if (区域 == null) continue;
                    int 破坏数量 = 检查区域中的避雷针(区域);
                    总破坏数量 += 破坏数量;
                }
                if (总破坏数量 > 0)
                {
                    Game1.addHUDMessage(new HUDMessage($"雷电摧毁了 {总破坏数量} 个避雷针！", 3));
                    监视器.Log($"雷电摧毁了 {总破坏数量} 个避雷针", LogLevel.Info);
                }
            }
            catch (System.Exception ex)
            {
                监视器.Log($"检查避雷针破坏时出错: {ex.Message}", LogLevel.Error);
            }
        }

        private int 检查区域中的避雷针(GameLocation 区域)
        {
            int 破坏数量 = 0;
            var 要移除的位置 = new List<Vector2>();
            foreach (var (位置, 物体) in 区域.Objects.Pairs)
            {
                if (物体 != null)
                {
                    if (物体.ParentSheetIndex == 9 || 物体.Name.Contains("Lightning Rod"))
                    {
                        if (Game1.random.NextDouble() < 破坏概率)
                        {
                            要移除的位置.Add(位置);
                            破坏数量++;
                        }
                    }
                }
            }
            foreach (var 位置 in 要移除的位置)
            {
                破坏避雷针(区域, 位置);
            }

            return 破坏数量;
        }

        private void 破坏避雷针(GameLocation 区域, Vector2 位置)
        {
            try
            {
                区域.Objects.Remove(位置);
                区域.playSound("breakingGlass");
                监视器.Log($"避雷针被雷电摧毁，位置: {位置}", LogLevel.Info);

            }
            catch (Exception ex)
            {
                监视器.Log($"破坏避雷针时出错: {ex.Message}", LogLevel.Error);
            }
        }
    }
}